<template>
    <div class="outer">
        <img class="back" src="@/assets/img/bg.jpg" alt="">

        <div class="main">

            <div class="title">富贵泉-用户体验调查</div>
            <span class="tip">尊敬的用户:<br>
                您好！为了给您提供更好的产品和服务，我们希望收集您使用富贵泉电动三轮车的看法和建议。对您的配合和支持表示由衷的感谢！</span>

            <div class="select-area">
                <span> 请选择城市：</span>
                <el-select v-model="city" placeholder="请选择城市" class="type-select">
                    <el-option :label="i" :value="i" v-for="(i, index) in options.city" :key="index"></el-option>
                </el-select>
            </div>

            <div class="area">
                <span>1.在你访问富贵车业时，发现好或不好的地方，请在此留言！我们会关注您的反馈，不断优化产品，为您提供更好的服务！</span>
                <textarea name="" id="" v-model="content"></textarea>
            </div>


            <div class="upload">
                <span> 2.为了便于我们核查问题，可上传遇到问题的截图。</span>
                <div class="up_btn" @click="triggerUpload">
                    <div>
                        <i class="el-icon-upload2" />
                        <span>上传图片</span>
                    </div>
                    <span>建议5MB以内（支持多图）</span> <!-- 修改3：添加支持多图提示 -->
                </div>

                <input type="file" ref="fileInput" multiple accept="image/*" @change="handleFileChange"
                    style="display: none;">

                <!-- 修改5：添加图片预览区域 -->
                <div class="preview-container" v-if="previewImages.length > 0">
                    <div class="preview-item" v-for="(image, index) in srcList" :key="index">
                        <el-image :src="image" :preview-src-list="srcList">
                        </el-image>
                        <span class="delete-btn" @click="removeImage(index)">×</span>
                    </div>
                </div>
            </div>


            <div class="phone">
                <span>3.为了方便我们与您联系，可填写您的手机号。(选填，信息仅作为内部资料绝不外泄)</span>
                <input type="text" v-model="phone">
            </div>
        </div>
        <div class="btn">
            <el-button type="primary" @click="addFeedback">提交</el-button>

        </div>
    </div>
</template>

<script>


import axios from 'axios';
import { baseUrl } from '@/api/index'

export default {
    data() {
        return {
            city: "",
            previewImages: [],  // 存储预览图片
            imgs: [], // 提交的图片数组为
            content: '',
            phone: '', // 手机号联系方式
            isFirst: false
        }
    },
    computed: {
        srcList() {
            let list = []
            this.previewImages.forEach(a => {
                list.push(a.url)
            })
            console.log(list.at, ';ist');

            return list
        },
        options() {
            return this.$store.state.dataInfo
        },
    },
    methods: {

        addFeedback() {
            if (this.isFirst) {
                this.$message.closeAll()
                this.$message({
                    type: 'error',
                    message: '等等！您的手速太快啦,请勿重复提交',
                })
                return
            }

            // 图片路径数组
            let { imgs, content, phone, city } = this
            let params = {
                imgs, content, phone, city
            }

            this.$api.addFeedback(params).then(res => {

                if (res.code == 1) {
                    this.isFirst = true

                    this.$message.closeAll()

                    this.$message({
                        type: 'success',
                        message: res.msg,
                    })
                    this.imgs = []
                    this.previewImages = []
                    this.content = ''
                    this.phone = ''
                    this.city = ''
                    this.isFirst = false
                }
            })
        },

        // 触发文件选择
        triggerUpload() {
            this.$refs.fileInput.click()
        },

        // 处理文件选择
        handleFileChange(event) {
            var that = this
            const files = event.target.files
            if (!files) return

            for (let i = 0; i < files.length; i++) {
                const file = files[i]

                // 检查文件大小
                if (file.size > 5 * 1024 * 1024) {
                    alert(`图片 ${file.name} 超过5MB限制`)
                    continue
                }

                // 检查是否已经存在相同名称和大小的文件（简单重复检查）
                const isDuplicate = this.previewImages.some(
                    img => img.file.name === file.name && img.file.size === file.size
                );

                if (isDuplicate) {
                    alert(`图片 ${file.name} 已经存在`);
                    continue;
                }

                // 生成预览
                const reader = new FileReader()
                reader.onload = (e) => {
                    this.previewImages.push({
                        url: e.target.result,
                        file: file
                    })

                    var formdata = new FormData();
                    formdata.append("file", file, file.name) //通过append向form对象添加

                    let config = {
                        headers: {
                            "Content-Type": "multipart/form-data",
                        }
                    }

                    axios.post(baseUrl + "user/upload/one", formdata, config).then(function (res) {
                        console.log("image", res)
                        if (res.data.code == 0) {
                            return false;
                        }

                        if (res.data.code == 1) {
                            that.imgs.push(res.data.data.filepath)
                        }
                    }).catch(function (error) {
                        console.log("error", error);
                        return false;
                    })

                }
                reader.readAsDataURL(file)
            }

            // 重置input
            event.target.value = ''
        },

        // 删除图片
        removeImage(index) {
            this.previewImages.splice(index, 1)
            this.imgs.splice(index, 1)
        }
    }
}
</script>

<style lang="scss" scoped>
.back {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    z-index: -1;
}

.main {
    position: relative;
    box-sizing: border-box;
    padding: 30px 120px 80px;
    margin-top: 140px;
    width: 1000px;
    background-color: #fff;
    border-radius: 40px;
    line-height: 25px;
    font-size: 16px;

    .title {
        text-align: center;
        font-size: 26px;
        font-weight: bold;
    }

    .tip {
        display: block;
        margin-top: 30px;
        color: #b4b2b2;
    }

    /* 新增：下拉选择框样式 */
    .select-area {
        margin-top: 20px;
    }

    .select-area span {

        margin-bottom: 8px;
        font-weight: bold;
        color: #333;
    }

    .type-select {
        width: 50%;
    }

    .area {
        margin-top: 40px;

        textarea {
            padding: 20px;
            margin: 20px;
            border-radius: 10px;
            width: calc(100% - 40px);
            height: 200px;
            resize: none;
        }
    }

    .upload {
        margin-top: 20px;

        .up_btn {
            cursor: pointer;
            /* 修改8：添加手型光标 */
            margin-top: 10px;
            display: flex;
            align-items: center;

            >div {
                padding: 3px 10px;
                display: flex;
                align-items: center;
                justify-content: center;
                border: 1px solid #7088f0;
                font-size: 13px;
                border-radius: 15px;
                margin-right: 10px;
                color: #7088f0;
            }

            >span {
                font-size: 13px;
                color: #777777;
            }
        }

        .up_btn:hover {
            border-color: #409eff;
        }


        /* 修改9：添加预览区域样式 */
        .preview-container {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 10px;
        }

        .preview-item {
            position: relative;
            width: 100px;
            height: 100px;
            border: 1px solid #eee;
            border-radius: 4px;

            ::v-deep .el-image {
                width: 100px;
                height: 100px
            }
        }

        .preview-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .delete-btn {
            position: absolute;
            right: 0;
            top: 0;
            background: rgba(0, 0, 0, 0.5);
            color: white;
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            cursor: pointer;
            border-radius: 0 0 0 4px;
        }

    }
}

.outer {
    position: relative;

    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    .btn {
        margin: 40px 0;

        >button {
            width: 120px;
        }
    }
}

.phone {
    margin-top: 40px;

    input {
        width: 100%;
        border: 1px solid #9c9999;
        height: 35px;
        font-size: 20px;
        margin-top: 20px;
        padding: 0 15px;
        outline: none;
    }
}

@media screen and (max-width: 768px) {
    .outer {
        background-color: #cbdbed;

        .back {
            display: none;
        }

        .btn {
            margin: 40px 0;

            >button {
                font-size: 15px;
                height: 30px;
                width: 100px;
            }
        }
    }

    .main {
        position: relative;
        box-sizing: border-box;
        padding: 60px 20px;
        margin-top: 80px;
        width: 95%;
        background-color: #fff;
        border-radius: 40px;
        line-height: 25px;
        font-size: 16px;

        .title {
            text-align: center;
            font-size: 26px;
            font-weight: bold;
        }

        .tip {
            display: block;
            margin-top: 30px;
            color: #b4b2b2;
        }

    /* 修复下拉框选项文字大小 */
    .select-area ::v-deep .el-select .el-input__inner {
        width: 100% !important;
        font-size: 16px !important; /* 增加输入框文字大小 */
        height: 44px;
        line-height: 44px;
    }
    
    /* 修复下拉选项列表的文字大小 */
    .select-area ::v-deep .el-select-dropdown {
        width: auto !important;
        min-width: 200px;
    }
    
    .select-area ::v-deep .el-select-dropdown__item {
        font-size: 16px !important; /* 增加选项文字大小 */
        padding: 0 20px;
        height: 44px !important; /* 增加选项高度 */
        line-height: 44px !important;
    }
    
    .select-area ::v-deep .el-select-dropdown__item.hover {
        background-color: #f5f7fa;
    }
    
    .select-area ::v-deep .el-select-dropdown__item.selected {
        color: #409eff;
        font-weight: 700;
    }
    
    /* 确保下拉框图标大小合适 */
    .select-area ::v-deep .el-select .el-input__suffix .el-icon-arrow-up:before {
        font-size: 16px;
    }

        .area {
            margin-top: 40px;

            textarea {
                font-size: 16px;
                padding: 10px;
                box-sizing: border-box;
                margin: 20px 10px;
                border-radius: 10px;
                width: calc(100% - 10px);
                height: 200px;
            }
        }

        .upload {
            margin-top: 20px;

            .up_btn {
                margin-top: 10px;
                display: flex;
                align-items: center;



                >div {
                    padding: 3px 10px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    border: 1px solid #7088f0;
                    font-size: 13px;
                    border-radius: 15px;
                    margin-right: 10px;
                    color: #7088f0;
                }

                >span {
                    font-size: 13px;
                    color: #777777;
                }
            }
        }


    }

    .phone {
        margin-top: 50px;

        input {
            width: 100%;
            border: 1px solid #9c9999;
            height: 35px;
            font-size: 16px;
            margin-top: 40px;
            padding: 0 8px;
            outline: none;
            border-radius: 10px;
        }
    }

    .preview-item {
        position: relative;
        width: 80px !important;
        height: 80px !important;
        border: 1px solid #eee;
        border-radius: 4px;

        ::v-deep .el-image {
            width: 80px !important;
            height: 80px !important;
        }
    }

    .delete-btn {
        position: absolute;
        right: 10px !important;
        top: 10px !important;
        background: rgba(0, 0, 0, 0.5);
        color: white;
        width: 20px;
        height: 20px;
        text-align: center;
        line-height: 20px;
        cursor: pointer;
        border-radius: 0 0 0 4px;
    }

}
</style>